#include "Log.h"


#define BUFFSIZE 100

using namespace std;
Log* Log::instance = NULL;

Log::Log() {
	logActivo = false;
}

Log::~Log() {
}

Log* Log::getInstance(){
	if (instance == NULL) {
		instance = new Log();
	}
	return instance;
}

void Log::inicializar(string nombre) {
	fileName = "log_" + nombre;
	logActivo = true;
}

void Log::destroy() {
	if (instance != NULL) {
		delete instance;
		instance = NULL;
	}
}

void Log::log(string mensaje) {
	if(logActivo){
		time_t rawtime;
		time ( &rawtime );
		string time = ctime (&rawtime);
		if (time[time.length()-1] == '\n'){
			time = time.substr(0,time.length()-1);
		}

		stringstream sstream;
		sstream << time << " :: PID: " << getpid() << " :: " << mensaje << endl;
		LockFile miLock(this->fileName.c_str());
		miLock.tomarLock();
		miLock.escribir(sstream.str().c_str(), sstream.str().size());
		miLock.liberarLock();
	}
}
